因為過節,產品頁面分三天寫;
後續依狀況會回頭整併在一起。
public Repository(ApplicationDbContext db)
{
_db = db;
this.dbSet = _db.Set<T>();
_db.Products.Include(u => u.Category).Include(u => u.CategoryId);
}
public T Get(Expression<Func<T, bool>> filter, string? includeProperties = null)
{
IQueryable<T> query = dbSet;
query = query.Where(filter);
if (!string.IsNullOrEmpty(includeProperties))
{
foreach (var includeProp in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProp);
}
}
return query.FirstOrDefault();
}
public IEnumerable<T> GetAll(Expression<Func<T, bool>>? filter, string? includeProperties = null)
{
IQueryable<T> query = dbSet;
if (filter != null)
{
query = query.Where(filter);
}
if (!string.IsNullOrEmpty(includeProperties))
{
foreach (var includeProp in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProp);
}
}
return query.ToList();
}
IEnumerable<T> GetAll(Expression<Func<T, bool>>? filter = null, string? includeProperties = null);
T Get(Expression<Func<T, bool>> filter, string? includeProperties = null);
public IProductRepository Product { get; private set; }
Product = new ProductRepository(_db);
IProductRepository Product { get; }
Product新增CURD
1.複製View底下Category資料夾,修改資料夾名稱為Product。
2.將Product底下的Upsert、Index.cshtml打開,
選取Category=>Ctrl+Shift+F 用 Product 針對開啟的文件取代即可。
3.取代完畢後,依照Product的Model修改View。
Product/Index.cshtml
<table id="productTbl" class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Category.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Size)
</th>
<th>
@Html.DisplayNameFor(model => model.Price)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th>Action</th>
</tr>
</thead>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Category.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Size)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>